<template>
  <el-card>
    <el-button @click="$router.push('/specs/add')" type="primary" icon="el-icon-circle-plus-outline">添加</el-button>
    <el-divider/>
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="specsname" label="属性名称" width="180"></el-table-column>
      <el-table-column prop="attrs" label="属性值">
        <template v-slot="scope">
          <el-tag type="success" v-for="item in scope.row.attrs" :key="item">{{item}}</el-tag>&nbsp;
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="error" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="mini"
            circle
            icon="el-icon-edit"
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            circle
            icon="el-icon-delete"
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @current-change="pageChange" :page-size="size" background layout="prev, pager, next" :total="total"></el-pagination>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      size: 1,
      total:0
    };
  },
  methods: {
    // 请求规格数据
    async getSpecs() {
      const res = await this.$http.get("/api/specslist", {
        params: { page: this.page, size: this.size }
      });
      if (res.code == 200) {
        this.list = res.list;
      }
      console.log(res);
    },
    // 获取数据总条数
    async getSpecsCount(){
        const res=await this.$http.get('/api/specscount');
        console.log(res);
        if(res.code==200){
            this.total=res.list[0].total;
        }
    },
    // 监听页码的更新
    pageChange(page){
        // 1-更新data中的page
        this.page=page;
        // 2-重新请求列表数据
        this.getSpecs();
    },
    edit(id) {},
    del(id) {}
  },
  created() {
    this.getSpecs();
    this.getSpecsCount();
  }
};
</script>

<style>
</style>